package cn.ljlao.mall.product.goods.service.impl;

import cn.ljlao.common.utils.PageUtils;
import cn.ljlao.common.utils.Query;
import cn.ljlao.mall.product.goods.dao.BrandDao;
import cn.ljlao.mall.product.goods.entity.BrandEntity;
import cn.ljlao.mall.product.goods.entity.CategoryBrandRelationEntity;
import cn.ljlao.mall.product.goods.service.BrandService;
import cn.ljlao.mall.product.goods.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("brandService")
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> brandEntityQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            brandEntityQueryWrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                brandEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 修改品牌分类及其关联分类
     * @param brand
     */
    @Override
    public void updateDetial(BrandEntity brand) {
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())){

            categoryBrandRelationService.updateByBrand(brand.getName(),brand.getBrandId());

            //TODO 待更新
        }
    }

}